package loaderData
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import animation.AnimationData;
	import animation.AnimationSys;

	/**
	 * 普通的动画加载 
	 * @author weiping
	 * 
	 */	
	public class LoaderAnimation extends BaseLoader
	{
		private var urlLoader:URLLoader;
		private var loader:Loader;
		
		private var frameNum:int;
		private var point:Point;
		
		public function LoaderAnimation()
		{
			urlLoader = new URLLoader();
			
			frameNum = 0;
			point = new Point();
		}
		
		override public function load(url:String):void
		{
			this.loaderUrl = url;
			urlLoader.addEventListener(Event.COMPLETE, onByteCompleteEvent);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoErrorEvent);
			urlLoader.addEventListener(Event.OPEN, onOpen);
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.load(new URLRequest(url));
		}
		
		protected function onByteCompleteEvent(event:Event):void
		{
			var bytes:ByteArray = urlLoader.data as ByteArray;
			
			//动画数量
			frameNum = bytes.readByte();
			//偏移
			var value:int = bytes.readInt();
			point.x = value;
			value = bytes.readInt();
			point.y = value;
			//帧数
			if(frameNum == 0)
			{
				throw new Error("动画帧数数量不能为零"+this.loadUrl);
			}
			
			var ba:ByteArray = new ByteArray();
			bytes.readBytes(ba, 0, bytes.bytesAvailable);
			bytes.clear();
			
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteEvent);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressEvent);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoErrorEvent);
			loader.loadBytes(ba);
			
		}
		
		override protected function onCompleteEvent(event:Event):void
		{
			this.objData = event.target.content as MovieClip;
			
			var vecData:Vector.<BitmapData> = new Vector.<BitmapData>;
			
			for(var j:int=0; j<frameNum;j++)
			{
				var cl:Class  = loader.contentLoaderInfo.applicationDomain.getDefinition("Frame_"  + j) as Class;
				var bit:BitmapData = new cl() as BitmapData;
				vecData.push(bit);
			}
			var data:AnimationData = new AnimationData();
			data.bitmapDataList = vecData;
			data.frameNum = vecData.length;
			data.offsetPos = new Point(point.x,point.y);
			
			AddAnimationData(this.loaderUrl, data);
			
			super.onCompleteEvent(event);
		}
		
		private function AddAnimationData(key:String, data:AnimationData):void
		{
			AnimationSys.inst.addAnimationData(key, data);
		}
		
		override protected function onIoErrorEvent(event:IOErrorEvent):void
		{
			super.onIoErrorEvent(event);
		}
		
		override protected function onProgressEvent(event:ProgressEvent):void
		{
			this.bytesLoadNum = event.bytesLoaded;
			this.bytesTotalNum = event.bytesTotal;
			super.onProgressEvent(event);
		}
		
		
		
		override public function clear():void
		{
			super.clear();
			frameNum = 0;
			point.x = point.y = 0;
			urlLoader.close();
			removeEvent();
		}

		protected function removeEvent():void
		{
			urlLoader.removeEventListener(Event.COMPLETE, onByteCompleteEvent);
			urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIoErrorEvent);
			urlLoader.removeEventListener(Event.OPEN, onOpen);
			
			if(this.loader)
			{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onCompleteEvent);
				loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgressEvent);
				loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onIoErrorEvent);
				loader.unload();
				loader = null;
			}	
		}
	}
}